//#define __FORCE_TRACE__
//#define __FORCE_DEBUG__
//#define __FORCE_INFO__
#define THIS_FILE "adminActions\fn_opc.sqf"
#include <core_macros.h>

if (!isServer) exitWith {};

TRACE_1("Start",_this);
TMFPROFILERSTART;

if (isNil "MSO_AA_PLAYER_ID_COUNT") then { MSO_AA_PLAYER_ID_COUNT = 2; };

private ["_name","_uid","_id","_index","_player_info","_previous_id","_player"];
PARAMS_3(_id,_name,_uid);
if (_name == "__SERVER__" || _uid == "__SERVER__") exitWith {TRACE("Ignoring the server");};

_index = GVAR(PlayerIndex) find _uid;
if (_index >= 0) then {
	_player_info = GVAR(PlayerInfo) select _index;
	
	_previous_id = _player_info select 2;
	if (_previous_id != _id) then {
		MSO_AA_PLAYER_ID_COUNT = MSO_AA_PLAYER_ID_COUNT + 1;
		TRACE_1("Updated Player ID",ARR_3(MSO_AA_PLAYER_ID_COUNT,_previous_id,_id));
	};
	
	_player_info set [0, _name];	// Player name at login
	_player_info set [1, _uid];		// Player UID
	_player_info set [2, _id];		// Player DirectPlay ID at last login
	_player_info set [3, MSO_AA_PLAYER_ID_COUNT];		// Player ID at login
	_player_info set [6, time];		// Player last login time
	
	TLOG_1("Updated",_player_info);
	GVAR(PlayerInfo) set [_index, _player_info];
	
	if ((_player_info select 9) > 0
		|| (_player_info select 8) > 3) then {
		[_uid,_name] spawn {
			private ["_uid","_name","_player","_auto_ban"];
			PARAMS_2(_uid,_name);
			sleep 5;
			_player = objNull;
			{ if (getPlayerUID _x == _uid) then { _player = _x; };	
			} foreach playableUnits;
			_auto_ban = [false,"ban",_player, _uid, _name];
			[QGVAR(BAN),[_auto_ban,"__SERVER__","__SERVER__"]] call FUNC(raiseEvent);
		};
	};
} else {
	MSO_AA_PLAYER_ID_COUNT = MSO_AA_PLAYER_ID_COUNT + 1;
	_index = count GVAR(PlayerIndex);
	GVAR(PlayerIndex) set [_index, _uid];
	_player = objNull;
	{ if (getPlayerUID _x == _uid) then { _player = _x; };	
	} foreach playableUnits;
	
	_player_info = [];
	_player_info set [0, _name];	// Player name at login
	_player_info set [1, _uid];		// Player UID
	_player_info set [2, _id];		// Player DirectPlay ID at last login
	_player_info set [3, MSO_AA_PLAYER_ID_COUNT]; // Player ID at login
	_player_info set [4, time];		// Player first login time
	_player_info set [5, []];// Initial Position
	_player_info set [6, time];		// Player last login time
	_player_info set [7, 0];		// Player jails
	_player_info set [8, 0];		// Player kicks
	_player_info set [9, 0];		// Player bans
	_player_info set [10, 0];		// Player teleports
	_player_info set [11, 0];		// Player vehicle crashes
	
	if (!isnull _player) then {
		_player_info set [5, getPos _player];
		TRACE_1("Position set",getPos _player);
	} else {
		_uid spawn {
			private ["_uid","_player","_index","_info"];
			_uid = _this;
			_player = objNull;
			while {isnull _player} do {
				{ if (getPlayerUID _x == _uid) then { _player = _x; };	
				} foreach playableUnits;
				sleep 0.5;
			};
			_index = GVAR(PlayerIndex) find _uid;
			if (_index >= 0) then {
				_info = GVAR(PlayerInfo) select _index;
				_info set [5, getPos _player];// Initial Position
				GVAR(PlayerIndex) set [_index, _info];
				TRACE_1("Position later set",getPos _player);
			};
		};
	};
	TLOG_1("Inserted",_player_info);
	GVAR(PlayerInfo) set [_index, _player_info];
};
publicVariable QGVAR(PlayerIndex);
publicVariable QGVAR(PlayerInfo);

TRACE_1("End",_index);
TMFPROFILERSTOP;
